import xml.etree.ElementTree as etree
import pandas as pd
import joblib
import os
import numpy as np
import matplotlib.pyplot as plt


#call this code from the directory that contains the data

outdir='data2020'
# os.makedirs('../'+outdir)

columns = ['glucose']




def split_list_by_n(list_collection, n):
	"""
    将集合均分，每份n个元素
    :param list_collection:
    :param n:
    :return:返回的结果为评分后的每份可迭代对象
    """
	for i in range(0, len(list_collection), n):
		yield list_collection[i: i + n]

if __name__=="__main__":
	filename = "cgm.txt"
	print('Processing {}...'.format(filename))

	patients = []
	total = 0
	raw_datas = []
	with open(filename) as f:
		lines = f.readlines()
		for line in lines:
			raw_datas.append(line.split("|"))
	cgm_data = {}
	cgm_min, cgm_max = 10000, 0
	for raw_data in raw_datas[1:]:
		patient_id = raw_data[0]
		cgm_min = min(cgm_min, int(raw_data[3]))
		cgm_max = max(cgm_max, int(raw_data[3]))
		if cgm_data.get(patient_id) == None:
			cgm_data[patient_id] = [np.asarray([pd.to_datetime(raw_data[2],dayfirst=True), int(raw_data[3])])]
		else:
			cgm_data[patient_id].append(np.asarray([pd.to_datetime(raw_data[2],dayfirst=True), int(raw_data[3])]))
	print("min: " + str(cgm_min) + " max: " + str(cgm_max))
	for patient_data in cgm_data.items():
		patient_id = patient_data[0]
		patient_data = np.asarray(patient_data)
		dict = {}
		len_data = len(patient_data[1])
		dict[''] = [data[0] for data in patient_data[1][:int(len_data*0.8)]]
		dict['glucose'] = [(data[1] - cgm_min) / cgm_max for data in patient_data[1][:int(len_data*0.8)]]
		#save data frame
		df_train=pd.DataFrame(dict)
		df_train.set_index('')
		joblib.dump(df_train,'../'+outdir+'/'+str(patient_id)+'train.pkl')

		dict[''] = [data[0] for data in patient_data[1][int(len_data * 0.8):]]
		dict['glucose'] = [(data[1] - cgm_min) / cgm_max for data in patient_data[1][int(len_data * 0.8):]]
		# save data frame
		df_test = pd.DataFrame(dict)
		df_test.set_index('')
		joblib.dump(df_test,'../'+outdir+'/'+str(patient_id)+'test.pkl')
